import axios from 'axios'
import store from '../store'
import router from '../router'

let baseUrl="/api"

const instance=axios.create({
    baseURL:baseUrl,
    timeout:10000,
    responseType:"json",
    withCredentials:false,
    headers:{
        "Content-Type":"application/json"
    }
});
instance.interceptors.request.use(function(config){
    //在发送请求之前做什么
    // console.log(config);
    //自定义一个字段，用来标识 是否需要token--CheckToken
    // window.localStorage.setItem("flag","loading");
    if (config.data.checkToken) {
        console.log(config)
        console.log(store.state.login.loginStatus)
        if (store.state.login.loginStatus) {
          
            config.headers['token']=store.state.login.token
        } else {
            console.log('你还没有登录');
            router.replace({
                name:'Login'
            })
        }
    }
    return config;
},function (error){
    //对请求错误做些什么
    return Promise.reject(error)
});


instance.interceptors.response.use(function(response){
    //对响应数据做点什么
    // console.log(response);
    // window.localStorage.removeItem("flag");
    return response;
},function (error){
    //对相应错误做点什么
    return Promise.reject(error)
})

export default instance